import os
import jinja2
import yaml
from pprint import pprint
from jinja2 import Template
import sys

args = sys.argv
if(args.__len__() == 5):
        yaml_file = args[1]
        module = args[2]
        rwtype = args[3]
        directory = args[4]
        print(args)
elif(args.__len__() == 4):     
        yaml_file = args[1]
        module = args[2]
        rwtype = args[3]
        directory = ''
        print(args)        
else:
        print('***********')
        exit()
with open(yaml_file, 'r') as f:
    cfg = yaml.safe_load(f)
def render(tpl1, cfg):
    path, fn1 = os.path.split(tpl1)
    return jinja2.Environment(loader=jinja2.FileSystemLoader(path or './')).get_template(fn1).render(cfg)
pprint(cfg)
for i in cfg:
    if 'hosts' in i:  
      jjjjjj = i['hosts']
      ary_change = []
      aaaa = jjjjjj['number']
      changes = jjjjjj['changes']
      bbbbb = []
      mmmm = jjjjjj['modules']
      print(mmmm)
      for change_pra in changes:   
        host = changes[change_pra]   
        print(host)
        start = 0
        end = 0
        ary_host = []
        for ccccccc in host: 
          if 'var' in ccccccc:
            start_end = ccccccc['var']
            print(start_end)
            array_var = start_end.split('-')
            print(array_var[0])
            print(array_var[1])
            start = int(array_var[0])
            end = int(array_var[1])
        for c in range(start , end+1):  
          hostval = ''
          for ccccccc in host: 
            if 'var' in ccccccc:
              hostval += str(c)
            else:
              aaa = list(ccccccc.values())[0]
              hostval += aaa
          print(hostval)    
          temp_dit = {change_pra: hostval}
          ary_host.append(temp_dit)
        print(ary_host)
        ary_change.append(ary_host)
      print(ary_change)  

      j = 0
      while j < int(aaaa):
        temphost = {}
        for i in ary_change:
          print(i[j])
          temphost.update(i[j])
        for k in jjjjjj:
          print(k)
          if k != 'changes' and k != 'number':
            tmp_dit = {k: jjjjjj[k]}
            temphost.update(tmp_dit)
        j += 1
        bbbbb.append(temphost)
      print(bbbbb)  

      for m in bbbbb:
         print(m)
         rt = ''
         if module != 'none':
            rt = render('module/' + module + '.txt',m)
         elif  mmmm:
            for j in mmmm:
              rt +=   render('module/' + j + '.txt',m)
         else:
            rt = 'error'     
         print(rt)
         if 'host' in m:
           if directory != '':
            if not os.path.exists(directory):
              os.mkdir(directory)
           if rwtype == 'a':
            cfgfile = open('./'+ directory + '/' + m['host']+'.txt','a')
           if rwtype == 'w':
            cfgfile = open('./'+ directory + '/' + m['host']+'.txt','w')
           cfgfile.write(rt)
           cfgfile.close()
    else:
      mmmm = i['modules']
      rt = ''
      if module != 'none':
        rt = render('module/' + module + '.txt',i)
      elif  mmmm:  
        for j in mmmm:
          rt +=   render('module/' + j + '.txt',i)
      else:
        rt = 'error'    
      print(rt)
      #if i.has_key('host'):  #Python2
      if 'host' in i:
        if directory != '':
          if not os.path.exists(directory):
            os.mkdir(directory)
        if rwtype == 'a':
          cfgfile = open('./'+ directory + '/' + i['host']+'.txt','a')
        if rwtype == 'w':
          cfgfile = open('./'+ directory + '/' + i['host']+'.txt','w')
        cfgfile.write(rt)
        cfgfile.close()
